﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DigitalSolutionLibrary.SqlDatabase;
using DigitalSolutionLibrary.Utilities;
using DigitalSolutionLibrary.Security;
using System.Threading;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Reflection;

namespace DigitalSolutionManagement
{
    public partial class frmLogIn : Form
    {
        private Thread th;

        private void LogIn()
        {
            ParameterStock stock = new ParameterStock();
            Parameter par = new Parameter("EmpCode", txtUserName.Text, SqlDbType.NVarChar, 50);
            stock.AddParamter(par);

            par = new Parameter("Password", TripleDES.Encryption(txtPassword.Text), SqlDbType.NVarChar, 100);
            stock.AddParamter(par);

            par = new Parameter("Message", null, SqlDbType.NVarChar, ParameterDirection.Output, 200);
            stock.AddParamter(par);

            par = new Parameter("Result", null, SqlDbType.Int, ParameterDirection.Output);
            stock.AddParamter(par);

            par = new Parameter("EmpId", null, SqlDbType.Int, ParameterDirection.Output);
            stock.AddParamter(par);

            object obj = Program.dbWrapper.ExecuteStore("Login", stock, DatabaseWrapper.ReturnType.DATASET);
            if (obj != null)
            {
                if ((int)stock.GetAtPosition(3).ValueOut > 0)
                {
                    Program.dtPermission = ((DataSet)obj).Tables[0];
                    Program.EmpId = (int)stock.GetAtPosition(4).ValueOut;
                    DialogResult = System.Windows.Forms.DialogResult.OK;
                }
                else
                {
                    pictureBox1.Visible = false;
                    txtPassword.Enabled = true;
                    txtUserName.Enabled = true;
                    panel2.Enabled = true;
                    lblMessage.Text = stock.GetAtPosition(2).ValueOut.ToString();
                }
            }
            else
            {
                pictureBox1.Visible = false;
                txtPassword.Enabled = true;
                txtUserName.Enabled = true;
                panel2.Enabled = true;
                lblMessage.Text = "Không thể kết nối đến server, vui lòng thử lại sau";
            }
        }

        private void StartLogin()
        {
            if (txtPassword.Text == String.Empty || txtUserName.Text == String.Empty)
            {
                lblMessage.Text = "Vui lòng nhập mã nhân viên và mật khẩu";
                lblMessage.Visible = true;
                return;
            }

            pictureBox1.Visible = true;
            lblMessage.Text = "Đang kiểm tra thông tin đăng nhập...";
            lblMessage.Visible = true;
            txtPassword.Enabled = false;
            txtUserName.Enabled = false;
            panel2.Enabled = false;

            th = new Thread(() => LogIn());
            th.Start();
        }

        public frmLogIn()
        {
            CheckForIllegalCrossThreadCalls = false;
            InitializeComponent();
            this.Icon = Properties.Resources.ico;
        }

        private void panel2_MouseDown(object sender, MouseEventArgs e)
        {
            panel2.BackgroundImage = Properties.Resources.btn_login_press;
        }

        private void panel2_MouseUp(object sender, MouseEventArgs e)
        {
            panel2.BackgroundImage = Properties.Resources.btn_login;
        }

        private void txtPassword_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                panel2.BackgroundImage = Properties.Resources.btn_login_press;
            }
        }

        private void txtPassword_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                panel2.BackgroundImage = Properties.Resources.btn_login;
                StartLogin();
            }
        }

        private void frmLogIn_Load(object sender, EventArgs e)
        {
            panel2.BackgroundImage = Properties.Resources.btn_login;
            lblVersion.Text = "Ver: " + Assembly.GetExecutingAssembly().GetName().Version.ToString();
        }

        private void txtUserName_KeyDown(object sender, KeyEventArgs e)
        {

            if (e.Control && e.KeyCode == Keys.D)
            {
                frmDBConfig frm = new frmDBConfig();
                frm.ShowDialog();
            }
            if (e.KeyCode == Keys.Enter)
            {
                panel2.BackgroundImage = Properties.Resources.btn_login_press;
            }
        }

        private void txtUserName_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                panel2.BackgroundImage = Properties.Resources.btn_login;
                StartLogin();
            }
        }

        private void panel2_Click(object sender, EventArgs e)
        {
            StartLogin();
        }

        private void frmLogIn_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (th != null && th.IsAlive)
                th.Abort();
        }
    }
}
